// pages/richeditor/richeditor.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgIndex: 0,
    firstCon: null,
    dataList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var iit = wx.getStorageSync('iit');
    if (!!iit) {
      this.setData({
        firstCon: iit.ftext,
        dataList: iit.imgs
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 输入监听
   */
  inputCon: function(e) {
    if (0 === e.currentTarget.id - 0) { //第一个文本框的输入监听
      this.data.firstCon = e.detail
    } else {
      this.data.dataList[e.currentTarget.id - 1].text = e.detail;
    }
  },
  /**
   * 失去焦点监听
   * 根据失去监听的input的位置来判断图片的插入位置
   */
  outBlur: function(e) {
    let that = this;
    that.data.imgIndex = e.currentTarget.id - 0;
  },
  /**
   * 添加图片
   */
  addImg: function() {
    var that = this;
    //这里考虑到性能，对于图片张数做了限制
    if (that.data.dataList.length >= 4) { //超过四张
      wx.showModal({
        content: '最多能添加四张图片哦！',
        confirmText: "知道了",
        showCancel: false
      })
    } else { //添加图片
      wx.chooseImage({
        sizeType: ['original', 'compressed'], //原图 压缩图
        sourceType: ['album', 'camera'], //相册 拍照
        count: 4 - that.data.dataList.length, //最多添加4张
        success: function(res) {
          res.tempFilePaths.forEach(function(x, i) {
            var info = {
              index: that.data.imgIndex + i,
              img: x, //存储本地地址
              text: null, //存储图片下方相邻的输入框的内容
            }
            that.data.dataList.splice(that.data.imgIndex + i, 0, info); //方法自行百度
          })
          that.setData({
            dataList: that.data.dataList,
          })
        }
      })
    }
  },
  /**
   * 删除图片
   */
  deletedImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      content: '移除图片？',
      success: function(res) {
        if (res.confirm) {
          if (index === 0 && !!that.data.dataList[index].text) { //删除第一张，要与最上方的textarea合并
            that.data.firstCon = that.data.firstCon + '\n' + that.data.dataList[index].text;
          } else if (index > 0 && !!that.data.dataList[index].text) {
            that.data.dataList[index - 1].text = that.data.dataList[index - 1].text + '\n' + that.data.dataList[index].text;
          }
          that.data.dataList.splice(index, 1);
          that.setData({
            firstCon: that.data.firstCon,
            dataList: that.data.dataList
          })
        }
      },
      fail: function(res) {
        console.log(res.errMsg)
      }
    })
  },
  onSave() {
    wx.removeStorageSync('iit')
    wx.setStorageSync('iit', {
      ftext: this.data.firstCon,
      imgs: this.data.dataList
    });
    wx.navigateBack({
      delta: 1
    })
  },
  onClear() {
    wx.removeStorageSync('iit');
    this.setData({
      imgIndex: 0,
      firstCon: null,
      dataList: []
    })
  }
})